package sqldbManagement.dbstudents;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import sqldbManagement.dbstudents.*;

public class Main {
	public String search="select * from student";
	public static void main(String[] args) {
		Main bb = new Main();
		bb.start();
	}
	ArrayList<StudentModel> list = new ArrayList<StudentModel>();//在外部定义，存储信息
	Scanner sc = new Scanner(System.in);
	public void start() {
		while(true) {
			System.out.println("=====欢迎使用学生档案管理======");
			System.out.println("=====键入数字使用功能======");
			System.out.println("----------1.录入学生信息");
			System.out.println("----------2.查看全部学生信息");
			System.out.println("----------3.按姓名查询学生");
			System.out.println("----------4.按专业查询学生");
			System.out.println("----------0.退出");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				try {
					inputs();
				} catch (SQLException e) {
					e.printStackTrace();
				}				//录入信息
				System.out.println("开始录入信息吧！");
				break;
			case 2:
				try {
					StudentsDao nn = new StudentsDao();
					nn.seeAll();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}				//查看所有学生的信息
				break;
			case 3:
				try {
					getOneByName();
				} catch (SQLException e) {
					e.printStackTrace();
				}			//按姓名查询
				break;
			case 4:
				try {
					getOneByPerfession();
				} catch (SQLException e) {
					e.printStackTrace();
				}		//按专业查询
				break;
			case 0:
				return;
			}
			System.out.println("*************是否退出系统?Y/N");
			if("Y".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}
//	public void start() {
//		while(true) {
//			System.out.println("=====欢迎使用学生档案管理======");
//			System.out.println("=====键入数字使用功能======");
//			System.out.println("----------1.录入学生信息");
//			System.out.println("----------2.查看全部学生信息");
//			System.out.println("----------3.按姓名查询学生");
//			System.out.println("----------4.按专业查询学生");
//			System.out.println("----------0.退出");
//			Scanner scanner = new Scanner(System.in);
//			switch (Integer.parseInt(sc.nextLine())) {
//			case 1:
//				try {
//					inputs();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}				//录入信息
//				System.out.println("开始录入信息吧！");
//				break;
//			case 2:
//				try {
//					StudentsDao nn = new StudentsDao();
//					nn.seeAll();
//				} catch (SQLException e1) {
//					e1.printStackTrace();
//				}				//查看所有学生的信息
//				break;
//			case 3:
//				try {
//					getOneByName(scanner);
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}			//按姓名查询
//				break;
//			case 4:
//				try {
//					getOneByPerfession();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}		//按专业查询
//				break;
//			case 0:
//				return;
//			}
//			System.out.println("*************是否退出系统?Y/N");
//			if("Y".equalsIgnoreCase(sc.nextLine()))
//				break;
//		}
//	}
	public void inputs() throws SQLException {			//控制多次输入
		while(true) {
			input();
			System.out.println("是否继续输入？Y/N");
			if("N".equalsIgnoreCase(sc.nextLine()))
				return;
		}

	}
	public void input() throws SQLException {			//控制单次输入
		StudentModel model = new StudentModel();	//model像一个工具盒，把每个小空间填满后，放到list中
		System.out.println("请输入name");
		model.setName(sc.nextLine());
		do {
			System.out.println("请输入uuid");
			model.setUuid(sc.nextLine());
		}while(isExist(model.getUuid()));
		
		System.out.println("请输入code");
		model.setCode(sc.nextLine());
		System.out.println("请输入gender");
		model.setGender(sc.nextLine());
		System.out.println("请输入birthday");
		model.setBirthday(sc.nextLine());
		System.out.println("请输入profession");
		model.setPerfession(sc.nextLine());
		System.out.println("请输入age");
		model.setAge(Integer.parseInt(sc.nextLine()));
		insert(model);
		model=null;

	}
	public boolean isExist(String uuid) throws SQLException {			//判断某个名字在集合中是否存在
		StudentsDao ss = new StudentsDao();
		return ss.isExistDao(uuid);
	}
	
	public void seeAll() throws SQLException {
		//代码转移到Dao中-FINISHED
	}
	public void getOneByName() throws SQLException {
		System.out.println("请输入要查找的姓名");
		String name = sc.nextLine();
		StudentsDao ss = new StudentsDao();
		ss.getOneByNameDao(name);
		//代码转移到Dao中-FINISHED
	}
	public void getOneByPerfession() throws SQLException {
		System.out.println("请输入要查找的专业");
		String profession = sc.nextLine();
		StudentsDao ss = new StudentsDao();
		ss.getOneByProfessionDao(profession);
		//代码转移到Dao中-FINISHED

	}
	public String insert(StudentModel model) throws SQLException {// add save
        String sql = "insert into student(uuid,name,code,gender,birthday,profession,age)values(?,?,?,?,?,?,?)";	//创建插入语句的sql语句
        Connection conn = JdbcUtil.getConnection();												//使用创建好的JdbcUtil获得数据库的的链接对象
        if (conn == null)
            return null;
        PreparedStatement ps = null;															//防止sql注入
        try {
            ps = conn.prepareStatement(sql);
            conn.setAutoCommit(false);
            ps.setString(1, model.getUuid());
            ps.setString(2, model.getName());
            ps.setString(3, model.getCode());
            ps.setString(4, model.getGender());
            ps.setString(5, model.getBirthday());
            ps.setString(6, model.getPerfession());
            ps.setInt(7, model.getAge());
            int res = ps.executeUpdate();
            conn.commit();
            return res + "";
        } catch (SQLException e) {
            e.printStackTrace();
            conn.rollback();
            try {
				conn.rollback();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
        } finally {
            JdbcUtil.close(conn, ps);
        }
        return null;
    }

   
}


